import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: hp
 * Date: 2024-08-08
 * Time: 12:19
 */
class MyThreadPool {
    private static BlockingQueue<Runnable> blockingQueue = new ArrayBlockingQueue<>(1000);
    public MyThreadPool(int n) {
        for(int i = 0; i < n; i++) {
            Thread t = new Thread(() -> {
                while(true) {
                    try {
                        Runnable runnable = blockingQueue.take();
                        runnable.run();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            });
            t.start();
        }
    }
    public void submit(Runnable runnable)  {
        try {
            blockingQueue.put(runnable);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

}
public class Demo1 {
    public static void main(String[] args)  {
        MyThreadPool pool = new MyThreadPool(4);
        for(int i = 0; i < 1000; i++) {
            int id = i;
            pool.submit(() -> {
                System.out.println("线程 " + id + "," + Thread.currentThread().getName());
            });
        }
    }
}
